

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="zh" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="zh" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>optuna.samplers._base &mdash; Optuna 1.4.0 文档</title>
  

  
  
    <link rel="shortcut icon" href="../../../_static/favicon.ico"/>
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script src="../../../_static/jquery.js"></script>
        <script src="../../../_static/underscore.js"></script>
        <script src="../../../_static/doctools.js"></script>
        <script src="../../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/css/custom.css" type="text/css" />
    <link rel="index" title="索引" href="../../../genindex.html" />
    <link rel="search" title="搜索" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html" class="icon icon-home"> Optuna
          

          
          </a>

          
            
            
              <div class="version">
                1.4.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">目录</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../installation.html">安装</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../tutorial/index.html">教程</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../reference/index.html">API Reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../faq.html">常见问题</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">Optuna</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">模块代码</a> &raquo;</li>
        
      <li>optuna.samplers._base</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>optuna.samplers._base 源代码</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">abc</span>

<span class="kn">from</span> <span class="nn">optuna</span> <span class="kn">import</span> <span class="n">type_checking</span>

<span class="k">if</span> <span class="n">type_checking</span><span class="o">.</span><span class="n">TYPE_CHECKING</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Any</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Dict</span>  <span class="c1"># NOQA</span>

    <span class="kn">from</span> <span class="nn">optuna.distributions</span> <span class="kn">import</span> <span class="n">BaseDistribution</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">optuna.study</span> <span class="kn">import</span> <span class="n">Study</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">optuna.trial</span> <span class="kn">import</span> <span class="n">FrozenTrial</span>  <span class="c1"># NOQA</span>


<div class="viewcode-block" id="BaseSampler"><a class="viewcode-back" href="../../../reference/samplers.html#optuna.samplers.BaseSampler">[文档]</a><span class="k">class</span> <span class="nc">BaseSampler</span><span class="p">(</span><span class="nb">object</span><span class="p">,</span> <span class="n">metaclass</span><span class="o">=</span><span class="n">abc</span><span class="o">.</span><span class="n">ABCMeta</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Base class for samplers.</span>

<span class="sd">    Optuna combines two types of sampling strategies, which are called *relative sampling* and</span>
<span class="sd">    *independent sampling*.</span>

<span class="sd">    *The relative sampling* determines values of multiple parameters simultaneously so that</span>
<span class="sd">    sampling algorithms can use relationship between parameters (e.g., correlation).</span>
<span class="sd">    Target parameters of the relative sampling are described in a relative search space, which</span>
<span class="sd">    is determined by :func:`~optuna.samplers.BaseSampler.infer_relative_search_space`.</span>

<span class="sd">    *The independent sampling* determines a value of a single parameter without considering any</span>
<span class="sd">    relationship between parameters. Target parameters of the independent sampling are the</span>
<span class="sd">    parameters not described in the relative search space.</span>

<span class="sd">    More specifically, parameters are sampled by the following procedure.</span>
<span class="sd">    At the beginning of a trial, :meth:`~optuna.samplers.BaseSampler.infer_relative_search_space`</span>
<span class="sd">    is called to determine the relative search space for the trial. Then,</span>
<span class="sd">    :meth:`~optuna.samplers.BaseSampler.sample_relative` is invoked to sample parameters</span>
<span class="sd">    from the relative search space. During the execution of the objective function,</span>
<span class="sd">    :meth:`~optuna.samplers.BaseSampler.sample_independent` is used to sample</span>
<span class="sd">    parameters that don&#39;t belong to the relative search space.</span>

<span class="sd">    The following figure depicts the lifetime of a trial and how the above three methods are</span>
<span class="sd">    called in the trial.</span>

<span class="sd">    .. image:: ../../image/sampling-sequence.png</span>

<span class="sd">    |</span>

<span class="sd">    &quot;&quot;&quot;</span>

<div class="viewcode-block" id="BaseSampler.infer_relative_search_space"><a class="viewcode-back" href="../../../reference/samplers.html#optuna.samplers.BaseSampler.infer_relative_search_space">[文档]</a>    <span class="nd">@abc</span><span class="o">.</span><span class="n">abstractmethod</span>
    <span class="k">def</span> <span class="nf">infer_relative_search_space</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study</span><span class="p">,</span> <span class="n">trial</span><span class="p">):</span>
        <span class="c1"># type: (Study, FrozenTrial) -&gt; Dict[str, BaseDistribution]</span>
        <span class="sd">&quot;&quot;&quot;Infer the search space that will be used by relative sampling in the target trial.</span>

<span class="sd">        This method is called right before :func:`~optuna.samplers.BaseSampler.sample_relative`</span>
<span class="sd">        method, and the search space returned by this method is pass to it. The parameters not</span>
<span class="sd">        contained in the search space will be sampled by using</span>
<span class="sd">        :func:`~optuna.samplers.BaseSampler.sample_independent` method.</span>

<span class="sd">        Args:</span>
<span class="sd">            study:</span>
<span class="sd">                Target study object.</span>
<span class="sd">            trial:</span>
<span class="sd">                Target trial object.</span>
<span class="sd">                Take a copy before modifying this object.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A dictionary containing the parameter names and parameter&#39;s distributions.</span>

<span class="sd">        .. seealso::</span>
<span class="sd">            Please refer to :func:`~optuna.samplers.intersection_search_space` as an</span>
<span class="sd">            implementation of :func:`~optuna.samplers.BaseSampler.infer_relative_search_space`.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">raise</span> <span class="ne">NotImplementedError</span></div>

<div class="viewcode-block" id="BaseSampler.sample_relative"><a class="viewcode-back" href="../../../reference/samplers.html#optuna.samplers.BaseSampler.sample_relative">[文档]</a>    <span class="nd">@abc</span><span class="o">.</span><span class="n">abstractmethod</span>
    <span class="k">def</span> <span class="nf">sample_relative</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study</span><span class="p">,</span> <span class="n">trial</span><span class="p">,</span> <span class="n">search_space</span><span class="p">):</span>
        <span class="c1"># type: (Study, FrozenTrial, Dict[str, BaseDistribution]) -&gt; Dict[str, Any]</span>
        <span class="sd">&quot;&quot;&quot;Sample parameters in a given search space.</span>

<span class="sd">        This method is called once at the beginning of each trial, i.e., right before the</span>
<span class="sd">        evaluation of the objective function. This method is suitable for sampling algorithms</span>
<span class="sd">        that use relationship between parameters such as Gaussian Process and CMA-ES.</span>

<span class="sd">        .. note::</span>
<span class="sd">                The failed trials are ignored by any build-in samplers when they sample new</span>
<span class="sd">                parameters. Thus, failed trials are regarded as deleted in the samplers&#39;</span>
<span class="sd">                perspective.</span>

<span class="sd">        Args:</span>
<span class="sd">            study:</span>
<span class="sd">                Target study object.</span>
<span class="sd">            trial:</span>
<span class="sd">                Target trial object.</span>
<span class="sd">                Take a copy before modifying this object.</span>
<span class="sd">            search_space:</span>
<span class="sd">                The search space returned by</span>
<span class="sd">                :func:`~optuna.samplers.BaseSampler.infer_relative_search_space`.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A dictionary containing the parameter names and the values.</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">raise</span> <span class="ne">NotImplementedError</span></div>

<div class="viewcode-block" id="BaseSampler.sample_independent"><a class="viewcode-back" href="../../../reference/samplers.html#optuna.samplers.BaseSampler.sample_independent">[文档]</a>    <span class="nd">@abc</span><span class="o">.</span><span class="n">abstractmethod</span>
    <span class="k">def</span> <span class="nf">sample_independent</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study</span><span class="p">,</span> <span class="n">trial</span><span class="p">,</span> <span class="n">param_name</span><span class="p">,</span> <span class="n">param_distribution</span><span class="p">):</span>
        <span class="c1"># type: (Study, FrozenTrial, str, BaseDistribution) -&gt; Any</span>
        <span class="sd">&quot;&quot;&quot;Sample a parameter for a given distribution.</span>

<span class="sd">        This method is called only for the parameters not contained in the search space returned</span>
<span class="sd">        by :func:`~optuna.samplers.BaseSampler.sample_relative` method. This method is suitable</span>
<span class="sd">        for sampling algorithms that do not use relationship between parameters such as random</span>
<span class="sd">        sampling and TPE.</span>

<span class="sd">        .. note::</span>
<span class="sd">                The failed trials are ignored by any build-in samplers when they sample new</span>
<span class="sd">                parameters. Thus, failed trials are regarded as deleted in the samplers&#39;</span>
<span class="sd">                perspective.</span>

<span class="sd">        Args:</span>
<span class="sd">            study:</span>
<span class="sd">                Target study object.</span>
<span class="sd">            trial:</span>
<span class="sd">                Target trial object.</span>
<span class="sd">                Take a copy before modifying this object.</span>
<span class="sd">            param_name:</span>
<span class="sd">                Name of the sampled parameter.</span>
<span class="sd">            param_distribution:</span>
<span class="sd">                Distribution object that specifies a prior and/or scale of the sampling algorithm.</span>

<span class="sd">        Returns:</span>
<span class="sd">            A parameter value.</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">raise</span> <span class="ne">NotImplementedError</span></div>

<div class="viewcode-block" id="BaseSampler.reseed_rng"><a class="viewcode-back" href="../../../reference/samplers.html#optuna.samplers.BaseSampler.reseed_rng">[文档]</a>    <span class="k">def</span> <span class="nf">reseed_rng</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Reseed sampler&#39;s random number generator.</span>

<span class="sd">        This method is called by the :class:`~optuna.study.Study` instance if trials are executed</span>
<span class="sd">        in parallel with the option ``n_jobs&gt;1``. In that case, the sampler instance will be</span>
<span class="sd">        replicated including the state of the random number generator, and they may suggest the</span>
<span class="sd">        same values. To prevent this issue, this method assigns a different seed to each random</span>
<span class="sd">        number generator.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">pass</span></div></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, Optuna Contributors.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
    <a href="../../../privacy.html">Privacy Policy</a>.
     


</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
    <!-- Theme Analytics -->
    <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-55135190-8', 'auto');
    ga('send', 'pageview');
    </script>

    
   

</body>
</html>